﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Qianwei.Model;
using Qianwei.Dac;
using Qianwei.Common;
using Qianwei.Common.Extension;
using MySql.Data.MySqlClient;

namespace Qianwei.Biz
{
    public class JobInfoManager : ManagerBase<JobInfoManager>
    {
        public IList<job_info> QueryJobInfo(string strWhere)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM job_info ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            IList<job_info> jilist = Mapper.DataReaderToList<job_info>(msh.ExecuteReader(strSql.ToString()));
            return jilist;
        }

        public IList<job_info> QueryJobInfo(string strWhere, string filedOrder)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM job_info ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            IList<job_info> jilist = Mapper.DataReaderToList<job_info>(msh.ExecuteReader(strSql.ToString()));
            return jilist;
        }

        public bool Delete(int Job_Id)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from job_info ");
            strSql.Append(" where Job_Id=@Job_Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Job_Id", (object)Job_Id)
            };

            int rows = msh.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool DeleteList(string Job_Idlist)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from job_info ");
            strSql.Append(" where ID in (" + Job_Idlist + ")  ");
            int rows = msh.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int Add(job_info model)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into job_info(");
            strSql.Append("Job_Position,Job_Address,Job_Treatment,Publish_Time,Effective_Duration,Job_Requirement,Create_Time,Last_Change_Time,Job_Number");
            strSql.Append(") values (");
            strSql.Append("@Job_Position,@Job_Address,@Job_Treatment,@Publish_Time,@Effective_Duration,@Job_Requirement,@Create_Time,@Last_Change_Time,@Job_Number");
            strSql.Append(") ");
            strSql.Append(";SELECT LAST_INSERT_ID();");
            MySqlParameter[] parameters = {
			            new MySqlParameter("@Job_Position", model.Job_Position) ,
                        new MySqlParameter("@Job_Address", model.Job_Address) ,
                        new MySqlParameter("@Job_Treatment", model.Job_Treatment) ,
                        new MySqlParameter("@Publish_Time", model.Publish_Time) ,
                        new MySqlParameter("@Effective_Duration", model.Effective_Duration) ,
                        new MySqlParameter("@Job_Requirement", model.Job_Requirement) ,
                        new MySqlParameter("@Create_Time", model.Create_Time) ,
                        new MySqlParameter("@Last_Change_Time", model.Last_Change_Time),
                        new MySqlParameter("@Job_Number", (object)model.Job_Number)
            };

            object obj = msh.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }

        public bool Update(job_info model)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update job_info set ");

            strSql.Append(" Job_Position = @Job_Position , ");
            strSql.Append(" Job_Address = @Job_Address , ");
            strSql.Append(" Job_Treatment = @Job_Treatment , ");
            strSql.Append(" Publish_Time = @Publish_Time , ");
            strSql.Append(" Effective_Duration = @Effective_Duration , ");
            strSql.Append(" Job_Requirement = @Job_Requirement , ");
            strSql.Append(" Last_Change_Time = @Last_Change_Time,  ");
            strSql.Append(" Job_Number=@Job_Number");
            strSql.Append(" where Job_Id=@Job_Id ");

            MySqlParameter[] parameters = {
			            new MySqlParameter("@Job_Id", model.Job_Id) ,            
                        new MySqlParameter("@Job_Position", model.Job_Position) ,
                        new MySqlParameter("@Job_Address", model.Job_Address) ,
                        new MySqlParameter("@Job_Treatment", model.Job_Treatment) ,
                        new MySqlParameter("@Publish_Time", model.Publish_Time) ,
                        new MySqlParameter("@Effective_Duration", model.Effective_Duration) ,
                        new MySqlParameter("@Job_Requirement", model.Job_Requirement) ,
                        new MySqlParameter("@Last_Change_Time", model.Last_Change_Time),
                        new MySqlParameter("@Job_Number", (object)model.Job_Number)
            };

            int rows = msh.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public job_info GetModel(int Job_Id)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Job_Id, Job_Position, Job_Address, Job_Treatment, Publish_Time, Effective_Duration, Job_Requirement, Create_Time, Last_Change_Time  ");
            strSql.Append("  from job_info ");
            strSql.Append(" where Job_Id=@Job_Id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Job_Id", (object)Job_Id)
            };

            job_info jobinfo = Mapper.DataReaderToObject<job_info>(msh.ExecuteReader(strSql.ToString(), parameters));
            return jobinfo;
        }

        public bool ExistsJobPosition(string jobPosition)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select 1 from job_info");
            strSql.Append(" where ");
            strSql.Append(" Job_Position = @Job_Position  ");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Job_Position", jobPosition)
            };

            object obj = msh.GetSingle(strSql.ToString(),parameters);
            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
